package org.argeproje.resim.proc.data;

import java.util.ArrayList;


public class KernelDA extends Data {
	   protected int _nCol;
	   protected int _nRow;
	   protected float [][] _kernel;
	   public KernelDA(float[][] kernel){
		   _nCol = kernel[0].length;
		   _nRow = kernel.length;
		   _kernel = kernel;
		   setDataType(Data.KERNEL);
	   }
	   public KernelDA(String kernelStringRepresentation){
		   String temp = kernelStringRepresentation;
		   if(temp.length()>1)
		   {
			   if((temp.charAt(0)=='[')&&(temp.charAt(temp.length()-1)==']'))
			   {
				   temp = temp.substring(1, temp.length()-1);
				   String[] rows= temp.split(";");
				   int rowIndex,colIndex;
				   rowIndex = 0;
				   _nRow = rows.length;
				   _kernel = new float[_nRow][];
				   for(String row : rows)
				   {
					   String[] cells = row.split(",");
					   _nCol = cells.length;
					   _kernel[rowIndex] = new float[_nCol];
					   colIndex = 0;
					   for(String cell : cells)
					   {
						   //try
						   //{
							   _kernel[rowIndex][colIndex++] = Float.parseFloat(cell);
						   /*}
						   catch(NumberFormatException e)
						   {
							   _kernel[rowIndex][colIndex++] = 0.0f;  
						   }*/
					   }
					   rowIndex++;
				   }
			   }
		   }
		   setDataType(Data.KERNEL);
	   }
	   public int getNCol(){
		   return _nCol;
	   }
	   public int getNRow(){
		   return _nRow;
	   }
	   public float [][] getKernel(){
		   return _kernel;
	   }
	   public float getValue(int rowIndex, int colIndex){
		   return _kernel[rowIndex][colIndex];
	   }
	   public void setValue(int rowIndex,int colIndex,float value){
		   _kernel[rowIndex][colIndex] = value;
	   }
	   
	   @Override
	   public String toString()
	   {
		   if((_kernel==null) || (_kernel.length==0))
		   {
			   return "[]";
		   }
		   else
		   {
			   StringBuilder sb = new StringBuilder();
			   //[ -2,3.5,6; 7,8, 9.0; 10,-11,12 ]
			   sb.append("[");
			   for(float[] items : _kernel)
			   {
				   sb.append(" ");
				   for(float item : items)
				   {
					   sb.append(item+",");
				   }
				   sb.setCharAt(sb.length()-1, ';');
			   }
			   sb.deleteCharAt(sb.length()-1);
			   sb.append(" ]");
			   return sb.toString();
		   }  
	   }
}
